var socketTask = null
Page({

  data: {
    schema: [{
      label: '发送内容',
      prop: 'data',
      type: 'text'
    }],
    formData: {
      data: ''
    },
    title: '',
    result: '',
    zh:'',
    apiType: '',
    remark: '',
    webUrl: '',
    apiName: '',
  },

  onLoad(options) {
    this.setData({
      apiType: options.zh,
      title: options.title,
      zh: options.zh,
      remark: options.remark,
      webUrl: options.webUrl
    })

    if (wx.onThemeChange) {
      wx.onThemeChange(({
        theme
      }) => {
        this.setData({
          theme
        })
      })
    }
  },
  getFormData(e) {
    console.log("de",e.detail)
    this.setData({
      formData: e.detail.formData
    })
  },
  connect() {
    socketTask = wx.connectSocket({
      url: 'ws://tczxt.dwsoft.com.cn/echat/socket/18610765207',
      success: (res) => {
        wx.onSocketMessage((result) => {
          this.setData({
            result,
            apiName: 'wx.onSocketMessage'
          })
        })
        wx.onSocketOpen((result) => {
          console.log(result)
          this.setData({
            result,
            apiName: 'wx.onSocketOpen'
          })
        })
        wx.onSocketError((result) => {
          console.log(result)
          this.setData({
            result,
            apiName: 'wx.onSocketError'
          })
        })
        wx.onSocketClose((result) => {
          console.log(result)
          this.setData({
            result,
            apiName: 'wx.onSocketClose'
          })
        })
      },
      complete: (res) => {
        this.setData({
          result: res,
          apiName: 'wx.connectSocket'
        })
      }
    })
  },
  sendMessage() {
    const data = this.data.formData
    console.log(data)
    wx.sendSocketMessage({
      data: data.data,
      complete: (res) => {
        this.setData({
          result: res,
          apiName: 'wx.sendSocketMessage'
        })
      }
    })
  },
  close() {
    wx.closeSocket({
      code:1000,
      reason:'手动关闭',
      complete:(res)=>{
        this.setData({
          result: res,
          apiName: 'wx.closeSocket'
        })
      }
    })
  },
  stSent() {
    const data = this.data.formData
    socketTask.send({
      data: data.data,
      complete:(res)=>{
        this.setData({
          result: res,
          apiName: 'SocketTask.send'
        })
      }
    })
  },
  stClose() {
    socketTask.close({
      code:1000,
      reason:'手动关闭',
      complete:(res)=>{
        this.setData({
          result: res,
          apiName: 'SocketTask.close'
        })
      }
    })
  }
})